/*

* ------------------------------------------------------------------------
* ------------------------------------------------------------------------
* |     Smart-Shop开源商城系统/ Java商城-首选电商平台系统 请务必保留此注释头信息
* |     开源地址: https://gitee.com/ningbo-qishan/gruul-mall
* ------------------------------------------------------------------------
* ------------------------------------------------------------------------
* |     可正常分享和学习源码,未经授权 不可商用！
* |     商业版中台购买/开源版商业授权 联系技术客服
* |     官网:  https://www.73app.cn/
* ------------------------------------------------------------------------
* ------------------------------------------------------------------------
* |     Copyright (c) 2016-2999 宁波启山智软科技有限公司
* |     版权所有 ,侵权必究！
* ------------------------------------------------------------------------

*/
package com.medusa.gruul.account.web.controller.api;


import cn.hutool.core.util.RandomUtil;
import com.medusa.gruul.account.conf.AccountRedis;
import com.medusa.gruul.account.constant.RedisConstant;
import com.medusa.gruul.account.constant.SmsConstant;
import com.medusa.gruul.account.model.vo.*;
import com.medusa.gruul.account.service.IMemberCardInfoService;
import com.medusa.gruul.common.core.annotation.EscapeLogin;
import com.medusa.gruul.common.core.constant.CommonConstants;
import com.medusa.gruul.common.core.constant.TimeConstants;
import com.medusa.gruul.common.core.exception.ServiceException;
import com.medusa.gruul.common.core.util.Result;
import com.medusa.gruul.payment.api.model.dto.PayResultDto;
import com.medusa.gruul.payment.api.model.dto.WxPayNotifyResultDto;
import com.medusa.gruul.sms.api.dto.SendSmsFeginDto;
import com.medusa.gruul.sms.api.feign.RemoteSmsSendService;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Calendar;
import java.util.List;

/**
 * @Description
 * @Author zhaokw
 * @Date 22:39 2020\5\30 0013
 **/
@RestController
@RequestMapping("/api")
public class ApiMemberCardInfoController {

    @Autowired
    private IMemberCardInfoService memberCardInfoService;
    @Autowired
    private RemoteSmsSendService remoteSmsSendService;

    @GetMapping("/getMemberCardInfo")
    @EscapeLogin
    @ApiOperation(value = "小程序获取会员卡信息")
    public Result<MemberCardInfoListVo> getMemberCardInfo() {
        MemberCardInfoListVo memberCardInfo = memberCardInfoService.getMemberCardInfo();
        return Result.ok(memberCardInfo);
    }

    @PostMapping("/confirmPayment")
    @ApiOperation(value = "小程序端购买弹框点击确认付款或确认兑换")
    public Result confirmPayment(@RequestBody @Validated ConfirmPaymentVo confirmPaymentVo) {
        PayResultDto payResultDto = memberCardInfoService.confirmPayment(confirmPaymentVo);
        return Result.ok(payResultDto);
    }

    /**
     * 购买会员卡支付跳转页面
     */
    @PostMapping("/confirmPayment/page")
    public Result<?> confirmPaymentPage(@RequestBody @Validated ConfirmPaymentPageVO confirmPaymentPage){
       return Result.ok(
               memberCardInfoService.confirmPaymentPage(confirmPaymentPage)
       );
    }

    /**
     * 发送验证码
     *
     * @param
     * @return
     */
    @GetMapping("/sendCode")
    @ApiOperation(value = "发送验证码")
    public Result sendCode(@ApiParam(value = "手机号", required = true) @RequestParam String phone) {
        //发送验证码
        String code = RandomUtil.randomNumbers(6);
        //验证码有效期未过则重新发送
        SendSmsFeginDto sendSmsFeginDto = new SendSmsFeginDto();
        sendSmsFeginDto.setSmsSendTime(Calendar.getInstance().getTimeInMillis());
        sendSmsFeginDto.setSmsSendMobiles(phone);
        sendSmsFeginDto.setSmsSendParam(code);
        sendSmsFeginDto.setSmsType(SmsConstant.SMS_TYPE_ALIYUN);
        sendSmsFeginDto.setSmsSendZone(SmsConstant.ZONE);
        sendSmsFeginDto.setSignId(SmsConstant.SIGN_ID);
        sendSmsFeginDto.setTemplateId(SmsConstant.TEMPLATE_ID);
        sendSmsFeginDto.setProviderId(SmsConstant.PROVIDER_ID);
        sendSmsFeginDto.setUserId(SmsConstant.USER_ID);
        Result order = remoteSmsSendService.createOrder(sendSmsFeginDto);
        if (order.getCode() != CommonConstants.SUCCESS) {
            throw new ServiceException("短信发送失败,".concat(order.getMsg()));
        }
        AccountRedis accountRedis = new AccountRedis();
        String key = RedisConstant.MEMBER_KEY.concat(phone);

        accountRedis.setNxPx(key, code, TimeConstants.FIVE_MINUTES);

        return Result.ok();
    }

    /**
     * @Description 小程序端购买会员卡填写信息
     * @Author zhaokw
     * @Date 10:47 2020\6\7 0007
     **/
    @PostMapping("/updateRegisterData")
    @ApiOperation(value = "小程序端购买会员卡填写信息")
    public Result<MemberCardInfoResVo> updateRegisterData(@RequestBody @Validated RegisterDataVo registerDataVo) {
        MemberCardInfoResVo memberCardInfoResVo = memberCardInfoService.updateRegisterData(registerDataVo);
        return Result.ok(memberCardInfoResVo);
    }

    /**
     * @Description 小程序端会员中心
     * @Author zhaokw
     * @Date 23:16 2020\6\10 0010
     **/
    @GetMapping("/memberCenter")
    @ApiOperation(value = "小程序会员中心")
    public Result<MemberCenterVo> memberCenter() {
        MemberCenterVo memberCenterVo = memberCardInfoService.memberCenter();
        return Result.ok(memberCenterVo);
    }

    /**
     * @Description 小程序端会员卡记录
     * @Author zhaokw
     * @Date 23:16 2020\6\10 0010
     **/
    @GetMapping("/getMemberCardRecord")
    @ApiOperation(value = "小程序端会员卡记录")
    public Result<List<MemberCardRecordVo>> getMemberCardRecord() {
        List<MemberCardRecordVo> memberCardRecordVos = memberCardInfoService.getMemberCardRecord();
        return Result.ok(memberCardRecordVos);
    }

    /**
     * @Description 获取租户下的注册表单信息，在小程序端确认付款之后
     * @Author zhaokw
     * @Date 23:16 2020\6\22 0010
     **/
    @GetMapping("/getRegisterForm")
    @ApiOperation(value = "获取租户下的注册表单信息")
    public Result<List<MemberRegisterFormVo>> getRegisterForm() {
        List<MemberRegisterFormVo> memberRegisterFormVos = memberCardInfoService.getRegisterForm();
        return Result.ok(memberRegisterFormVos);
    }

    @PostMapping("/memberCompleted")
    @ApiOperation(value = "测试用，会员充值")
    public Result memberCompleted(@RequestBody @Validated WxPayNotifyResultDto wxPayNotifyResultDto) {
        memberCardInfoService.memberCompleted(wxPayNotifyResultDto);
        return Result.ok();
    }


}
